package com.example.module_house.adapter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.module_house.R
import com.example.module_house.model.HouseEntity
import com.example.module_house.model.RegionGroup
class HouseAdapter1 :RecyclerView.Adapter<HouseAdapter1.HouseViewHolder2>() {
    private var regionGroups: List<RegionGroup> = emptyList()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HouseViewHolder2 {
        return HouseViewHolder2(
            LayoutInflater.from(parent.context).inflate(R.layout.item_house_one, parent, false)
        )
    }

    override fun getItemCount(): Int {
        return regionGroups.size
    }
   fun submitList(list: List<RegionGroup>) {
        this.regionGroups = list
        notifyDataSetChanged()
    }

    override fun onBindViewHolder(holder: HouseViewHolder2, position: Int) {
        val regionGroup = regionGroups[position]
        holder.bind(regionGroup)
        //避免在onBindViewHolder中创建新对象
        //使用稳定的ID（如果你的数据有唯一ID）
        //考虑使用DiffUtil进行高效更新
    }


   inner class HouseViewHolder2(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val tvRegionName: TextView = itemView.findViewById(R.id.qu1)
        val rvBuildings: RecyclerView = itemView.findViewById(R.id.rv2)
        val adapter = HouseAdapter()

       init {
           rvBuildings.layoutManager = GridLayoutManager(itemView.context, 2)
           rvBuildings.adapter = adapter
           //内层RecyclerView设置isNestedScrollingEnabled = false解决滑动冲突
           rvBuildings.isNestedScrollingEnabled = false // 重要！解决嵌套滑动冲突
       }

       fun bind(regionGroup: RegionGroup) {
           tvRegionName.text = "${regionGroup.regionId}栋"
           adapter.submitList(regionGroup.buildings)
       }
    }

}







